# PLOTS ------------------------------------------------------------------------
# This file outputs the plots displayed in the paper and in the appendix. 

# Create directory to save plots  ----------------------------------------------
path <- file.path(getwd(), paste0("plots/", experiment))
dir.create(path, showWarnings = FALSE)

# Plot contingency tables  -----------------------------------------------------
descr_at <- plot_descriptive("confidence_bin")
descr_rt <- plot_descriptive("support_bin")
descr_both <- ggarrange(descr_at, descr_rt)

ggsave("_plt-descr.pdf", path = path, width = 15, height = 7, dpi = 600)

# Plot ATE models --------------------------------------------------------------
ate_at <- plot_ate(at_ln$Main, 
                   caption = "OLS Models for\nConfidence in Attribution")
ate_rt <- plot_ate(rt_ln$Main, 
                   caption = "OLS Models for\nSupport for Retaliation")
ate_both <- ggarrange(ate_at, ate_rt, common.legend = TRUE, legend = "right")

ggsave("_plt-ate.pdf", path = path, height = 7.81, width = 13.4, dpi = 600)

# Plot ATE models for specific responses ---------------------------------------
models <- list("Do nothing" = st_do_nothing$OLS$Full,
               "Condemn" = st_do_condemn$OLS$Full,
               "Sanction" = st_do_sanction$OLS$Full,
               "Cyberespionage" = st_do_espionage$OLS$Full,
               "Air strike" = st_do_airstrike$OLS$Full)

models <- if (experiment == "1") {
  append(models, list("Cyberattack" = st_do_cyberattack$OLS$Full), after = 4)
} else if (experiment == "2") {
  append(models, list("Cyberattack Duma" = st_do_duma$OLS$Full, 
                      "Cyberattack Kremlin" = st_do_kremlin$OLS$Full), 
         after = 4)
} else if (experiment == "3") {
  append(models, list("Hack Power Grid" = st_do_powergrid$OLS$Full,
                      "Cyberattack" = st_do_cyberattack$OLS$Full), 
         after = 4)
}

ate_st <- plot_ate(models, 
                   caption = "Support for Specific Responses")

ggsave("_plt-ate-st.pdf", path = path, height = 7.81, width = 7.8, dpi = 600)


# Plot ATE for multilateral action (Experiment 3) ---------------------------
if(experiment == "3") {
  un1 <- plot_ate(st_un_nothing$OLS, caption = "Do Nothing")
  un2 <- plot_ate(st_un_condemn$OLS, caption = "Condemn")
  un3 <- plot_ate(st_un_sanction$OLS, caption = "Sanctions")
  un4 <- plot_ate(st_un_force$OLS, caption = "Intervene")
  ggarrange(un1, un2, un3, un4, common.legend = TRUE, legend = "right", nrow = 1)
  ggsave("_plt-unsc.pdf", path = path, height = 6, width = 18, dpi = 600)
}

# Plot CATE models -------------------------------------------------------------
cf_cate_at <- at_cate |>
  mapply(get_diff, model = _, mod = subgroups, SIMPLIFY = FALSE) |>
  lapply(as.data.frame) |>
  c(make.row.names = FALSE) |>
  do.call(rbind.data.frame, args = _) |>
  mutate(term = label)

plt_cate_at <- plot_cate(cf_cate_at, caption = "Confidence in Attribution")
if (experiment == 3) {
  plt_cate_at <- plot_cate(cf_cate_at, caption = "Confidence in Attribution",
                           xlim = c(-.7,.7))
}
ggsave(filename = "_plt-cate-at.pdf", plot = plt_cate_at, path = path, 
       height = 6, width = 14, dpi = 600)

cf_cate_rt <- rt_cate |>
  mapply(get_diff, model = _, mod = subgroups, SIMPLIFY = FALSE) |>
  lapply(as.data.frame) |>
  c(make.row.names = FALSE) |>
  do.call(rbind.data.frame, args = _) |>
  mutate(term = label)

plt_cate_rt <- plot_cate(cf_cate_rt, caption = "Support for Retaliation")
ggsave(filename = "_plt-cate-rt.pdf", plot = plt_cate_rt, path = path, 
       height = 6, width = 14, dpi = 600)
